package cn.kgc.shiro02.exception;

import cn.kgc.shiro02.commons.CodeMsg;
import cn.kgc.shiro02.commons.ResponseResult;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/4/1
 * @Description:
 */
@RestControllerAdvice
public class GlobalException {

    // 没有权限
    @ExceptionHandler(AuthorizationException.class)
    public ResponseResult authorizationExceptionHandler(AuthorizationException e){
        e.printStackTrace();
        return new ResponseResult(CodeMsg.USER_NO_AUTHORIZATION);
    }

    // 密码错误
    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult  incorrectCredentialsExceptionHandler(IncorrectCredentialsException e){
        e.printStackTrace();
        return new ResponseResult(CodeMsg.PASSWORD_ERROR);
    }

    // 账户不存在
    @ExceptionHandler(UnknownAccountException.class)
    public ResponseResult  unknownAccountExceptionHandler(UnknownAccountException e){
        e.printStackTrace();
        return new ResponseResult(CodeMsg.USER_NOT_EXIST);
    }

    // LockedAccountException 用户被禁用
    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult  lockedAccountExceptionHandler(LockedAccountException e){
        e.printStackTrace();
        return new ResponseResult(CodeMsg.USER_LOCKED);
    }


    @ExceptionHandler(Exception.class)
    public ResponseResult  exceptionHandler(Exception e){
        e.printStackTrace();
        return new ResponseResult(CodeMsg.ERROR);
    }

}
